package com.example.demo;

import com.example.demo.bean.GoodsPriceBean;
import com.example.demo.exception.ShoppingException;

import java.util.Optional;

public class ShoppingTrade implements DiscountSerivce, GoodsPriceSerivce{

    GoodsPriceBean goodsPriceBean = new GoodsPriceBean();
    @Override
    public Double discount(int sum, double disount) {
        Double sum1 = (Double) ((int) sum * disount);
        return sum1;
    }

    @Override
    public Double cost(Double sum, int floal) {

        return (sum >= 100) ? (sum - floal) : sum;
    }

    @Override
    public Double goodsprice(Integer goods, int count, Double discount) {
        Double price;
        price = Double.valueOf(discount(goods, discount)) * count;
        return price;
    }

    //计算总价
    private Double sum(Double apple, Double berry, Double mango) {
        return apple + berry + mango;
    }

    //参数检查
    private void beforeTrade(Integer appleCount, Integer berryCount, Integer mangoCount) throws ShoppingException {
        Optional.ofNullable(appleCount).orElseThrow(() ->new ShoppingException("000", "苹果价格为空！"));
        Optional.ofNullable(berryCount).orElseThrow(() ->new ShoppingException("000", "草莓商品价格为空！"));
        Optional.ofNullable(mangoCount).orElseThrow(() ->new ShoppingException("000", "芒果商品价格为空！"));
    }

    public Double shoppingcCost(Integer appleCount, Integer berryCount, Integer mangoCount){
        try {
            beforeTrade(appleCount, berryCount, mangoCount);
            Double priceOfApple = this.goodsprice(goodsPriceBean.getApple(), appleCount, 0.8);
            Double priceOfBerry = this.goodsprice(goodsPriceBean.getBerry(), berryCount, 1.0);
            Double priceOfMango = this.goodsprice(goodsPriceBean.getMANGO(), mangoCount, 0.9);
            Double sumCost = this.cost(sum(priceOfMango, priceOfApple, priceOfBerry), 10);
            return sumCost;
        } catch (ShoppingException e) {
            return Double.valueOf(e.getErrCode());
        }
    }


}
